<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <link rel="stylesheet" th:href="@{/static/element/index.css}"/>
</head>
<body>
<div id="app">
    <template>
        <el-card style="background-color: #f7f7f7">
            <el-form :model="queryForm" ref="queryForm" :inline="true" label-width="100px">
                <el-form-item label="KEY" prop="key">
                    <el-input
                            v-model="queryForm.key"
                            placeholder="请输入key"
                            clearable
                            size="small"
                            @keyup.enter.native="handleQuery"
                    />
                </el-form-item>
                <el-form-item label="名称" prop="name">
                    <el-input
                            v-model="queryForm.name"
                            placeholder="请输入名称"
                            clearable
                            size="small"
                            @keyup.enter.native="handleQuery"
                    />
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">清空</el-button>
                </el-form-item>
            </el-form>
        </el-card>

        <el-row style="margin-top: 1%">
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        icon="el-icon-plus"
                        size="mini"
                        @click="handleAddOrUpdate(1,null)"
                >
                    创建模型
                </el-button>
            </el-col>
        </el-row>


        <el-table
                v-loading="loading"

                :data="tableData" row-key="id"
                :header-cell-style="{'text-align':'center','color':'#303133','font-weight':'600','background-color': '#F5F7FA'}"
                :cell-style="{'text-align':'center'}" border style="margin-top: 10px"
        >
            <el-table-column label="ID" align="center" prop="id"></el-table-column>
            <el-table-column label="KEY" align="center" prop="key" show-overflow-tooltip="true"></el-table-column>
            <el-table-column label="名称" align="center" prop="name" show-overflow-tooltip="true"></el-table-column>
            <el-table-column label="描述" align="center" prop="metaInfo" show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <span type="success" size="small">{{scope.row.description}}</span>
                </template>
            </el-table-column>
            <el-table-column label="版本号" align="center" prop="version"></el-table-column>
            <el-table-column label="创建时间" align="center" prop="createTime"></el-table-column>
            <el-table-column label="最后更新时间" align="center" prop="lastUpdateTime"></el-table-column>
            <el-table-column label="操作" width="400">
                <template slot-scope="scope">
                    <el-button  size="mini" type="primary" icon="el-icon-edit"
                               @click="handleAddOrUpdate(2,scope.row)">绘制流程
                    </el-button>

                    <el-button size="mini" type="success" icon="el-icon-s-cooperation"
                               @click="deployModel(scope.row)">部署
                    </el-button>

                    <el-button size="mini" type="warning" icon="el-icon-download"
                               @click="exportModel(scope.row)">导出
                    </el-button>

                    <el-button  size="mini" type="info" icon="el-icon-delete"
                               @click="handleDelete(scope.row)">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                background
                :page-sizes="[10, 20, 30, 40,50]"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                :page-size.sync="pageSize"
                :current-page.sync="currentPage"
                @size-change="handleQuery"
                @current-change="handleQuery"
                style="float: right;margin-top: 10px"
        />
    </template>
</div>
</body>
<script th:src="@{/static/js/axios.min.js}"></script>
<script th:src="@{/static/js/request.js}"></script>
<script th:src="@{/static/js/common.js}"></script>
<script th:src="@{/static/api/workflow/model.js}"></script>
<script th:src="@{/static/js/vue.min.js}"></script>
<script th:src="@{/static/element/index.js}"></script>
<script th:src="@{/static/js/jquery.min.js}"></script>
<script th:src="@{/static/layer/layer.js}"></script>
<script>
    new Vue({
        el: '#app',
        data() {
            return {
                currentPage: 1,
                pageSize: 10,
                total: 0,
                tableData: [],

                button: {
                    add: false,
                    update: false,
                    delete: false
                },
                loading: false,
                queryForm: {}
            }
        },
        created() {
            this.handleQuery();
            let sysButtons = JSON.parse(localStorage.getItem("mars-sysButtons"));
            this.button.add = sysButtons.indexOf("admin/sysConfig/add") > -1;
            this.button.update = sysButtons.indexOf("admin/sysConfig/update") > -1;
            this.button.delete = sysButtons.indexOf("admin/sysConfig/delete") > -1;

        },
        methods: {
            resetQuery() {
                this.queryForm = {}
                this.handleQuery()
            },
            // 部署模型
            deployModel(row) {
                let params = {}
                params.modelId = row.id
                submitConfirm("/workflow/modeler/deploy", "部署流程", params, this)
            },
            // 导出模型
            exportModel(row) {
                let params = {}
                params.modelId = row.id
                exports('/workflow/modeler/export', params);
            },
            handleCurrentChange(val) {
                this.currentPage = val;
                this.handleQuery();
            },
            handleSizeChange(val) {
                this.pageSize = val;
                this.handleQuery();
            },
            handleQuery() {
                let queryParam = this.queryForm
                queryParam.pageNo = this.pageNo;
                queryParam.pageSize = this.pageSize;
                this.loading = true
                pageList(queryParam).then((res) => {
                    if (res.code === '200') {
                        this.tableData = res.data.list.map(item => {
                            return {
                                createTime: this.formatDate(item.createTime),
                                id: item.id,
                                name: item.name,
                                version: item.version,
                                lastUpdateTime: this.formatDate(item.lastUpdateTime),
                                key: item.key,
                                tenantId: item.tenantId,
                                description: JSON.parse(item.metaInfo).description
                            }
                        });
                        this.total = res.data.count;
                        this.loading = false
                    } else {
                        this.loading = false;
                        this.$notify({
                            title: '失败',
                            message: res.message,
                            type: 'error'
                        });
                    }
                });
            },
            handleDelete(row) {
                deleteById("/workflow/modeler/remove", {ids: row.id})
            },

            formatDate(timestamp) {
                const date = new Date(timestamp);
                const year = date.getFullYear();
                const month = ("0" + (date.getMonth() + 1)).slice(-2);
                const day = ("0" + date.getDate()).slice(-2);
                const hours = ("0" + date.getHours()).slice(-2);
                const minutes = ("0" + date.getMinutes()).slice(-2);
                const seconds = ("0" + date.getSeconds()).slice(-2);
                return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
            },

            handleAddOrUpdate(type, row) {
                if (type === 1) {
                    openPage('创建模型', '/workflow/modelAddUpdate.html', this);
                } else {
                    openFullPage('绘制流程', '/modeler/modeler.html?modelId=' + row.id, this);
                }
            },
        }
    });
</script>
</html>
